package library_system.com;

/**
 * @author haomin
 * @date 2022/04/05 22:09
 **/

import library_system.Util.DBUtil;
import library_system.model.Book;
import library_system.model.Reader;

import java.sql.*;
import java.util.Date;
import java.util.Scanner;

// 借书命令
public class BorrowBookCommandV2 {
    public static void main(String[] args) throws SQLException {
        Reader currentUser = new Reader();

        // 1. 登录
        // 1.1 证明你是你
        // 1.2 权限（角色）
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名: ");
        currentUser.name = scanner.nextLine();
        System.out.print("请输入密码: ");
        currentUser.password = scanner.nextLine();

        // select * from readers where name = ?
        try (Connection c = DBUtil.connection()) {
            String sql = "select rid from readers where name = ? and password = ?";
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setString(1, currentUser.name);
                ps.setString(2, currentUser.password);

                try (ResultSet rs = ps.executeQuery()) {
                    if (!rs.next()) {
                        System.out.println("登录失败");
                        return;
                    }

                    currentUser.rid = rs.getInt("rid");
                    System.out.println("登录成功");
                    System.out.println();
                }
            }
        }

        Book book = new Book();
        System.out.print("请输入书名: ");
        book.name = scanner.nextLine();
        // select 书籍有没有 && count > 0
        // insert records + update books
        try (Connection c = DBUtil.connection()) {
            String sql = "select bid, stock from books where name = ?";
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setString(1, book.name);

                try (ResultSet rs = ps.executeQuery()) {
                    if (!rs.next()) {
                        System.out.println("查无此书");
                        return;
                    }

                    book.bid = rs.getInt("bid");
                    book.stock = rs.getInt("stock");

                    if (book.stock == 0) {
                        System.out.println("书被借空了");
                        return;
                    }
                }
            }
        }

        // 借书操作
        try (Connection c = DBUtil.connection()) {
            String sql = "update books set stock = stock - 1 where bid = ?";
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, book.bid);

                ps.executeUpdate();
            }
        }
        try (Connection c = DBUtil.connection()) {
            String sql = "insert into records (rid, bid, borrow_at) values (?, ?, ?)";
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, currentUser.rid);
                ps.setInt(2, book.bid);
                // ps.setDate(3, new java.sql.Date(new java.util.Date().getTime()));
                ps.setTimestamp(3, new Timestamp(new Date().getTime()));

                ps.executeUpdate();
            }
        }
        System.out.println("借书成功！");
    }
}